package aftersale

import (
	"context"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAfterSaleByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 通过ID获取AfterSale
func NewGetAfterSaleByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAfterSaleByIdLogic {
	return &GetAfterSaleByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAfterSaleByIdLogic) GetAfterSaleById(req *types.AfterSaleReq) (resp *types.AfterSaleResp, err error) {
	if req.Id == 0 {
		return nil, resultx.NewErrCode(resultx.AFTER_SALE_ID_REQUIRED)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}
	resp = &types.AfterSaleResp{}
	// preload := "userAddress:Id,Label,Consignee,Mobile,Province,City,District,DetailedAddress,PostalCode,IsDefault,meta"
	var platformId uint32 = globalkey.ThirdPartyPlatformWechatMini
	data, err := l.svcCtx.OrderRpc.GetAfterSaleById(l.ctx, &orderclient.BaseIdReq{
		Id: pointy.GetPointer(req.Id),
		// Preload:    pointy.GetPointer(preload),
		VisitorId:  pointy.GetPointer(userId),
		PlatformId: pointy.GetPointer(platformId),
	})
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, resultx.NewErrCode(resultx.AFTER_SALE_ID_INVALID)
	}
	resp.Info = &types.AfterSaleInfo{
		Id:               data.Info.Id,
		AfterSaleSn:      data.Info.AfterSaleSn,
		OrderId:          data.Info.OrderId,
		OrderItemId:      data.Info.OrderItemId,
		OrderSn:          data.Info.OrderSn,
		ProductId:        data.Info.ProductId,
		ProductSkuId:     data.Info.ProductSkuId,
		ProductTitle:     data.Info.ProductTitle,
		UserId:           data.Info.UserId,
		Mobile:           data.Info.Mobile,
		AfterSaleType:    data.Info.AfterSaleType,
		RefundType:       data.Info.RefundType,
		ReturnType:       data.Info.ReturnType,
		RefundAmount:     data.Info.RefundAmount,
		RefundDetails:    data.Info.RefundDetails,
		Remark:           data.Info.Remark,
		ExpressSn:        data.Info.ExpressSn,
		ReasonForRefusal: data.Info.ReasonForRefusal,
		Status:           data.Info.Status,
		ProcessTime:      data.Info.ProcessTime,
		CreatedAt:        data.Info.CreatedAt,
		UpdatedAt:        data.Info.UpdatedAt,
		DeletedAt:        data.Info.DeletedAt,
	}
	if data.AddressInfo != nil {
		resp.AddressInfo = &types.CompanyAddressInfo{
			Id:             data.AddressInfo.Id,
			AddressDetails: data.AddressInfo.AddressDetails,
			ContactPerson:  data.AddressInfo.ContactPerson,
			Mobile:         data.AddressInfo.Mobile,
			IsDefault:      data.AddressInfo.IsDefault,
			DelStatus:      data.AddressInfo.DelStatus,
			Status:         data.AddressInfo.Status,
			CreatedAt:      data.AddressInfo.CreatedAt,
			UpdatedAt:      data.AddressInfo.UpdatedAt,
			DeletedAt:      data.AddressInfo.DeletedAt,
		}
	}
	return resp, nil
}
